/**
 * 
 */
package net.efo.controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Vector;

import net.efo.lib.Controller;
import net.efo.model.ConcreteDocument;
import net.efo.model.Document;
import net.efo.model.Tag;
import net.efo.model.sqlite.Statements;

/**
 * @author johnydoe
 * 
 */
public class DocumentController extends Controller {

	private Vector<Document> documents;
	private Vector<Tag> tags;

	private Statements stmt;

	public DocumentController(Statements stmt) {
		this.documents = new Vector<Document>();
		this.tags = new Vector<Tag>();
		this.stmt = stmt;
	}

	public void populateModels() {
		try {
			ResultSet rs;
			{
				// Tags
				rs = stmt.query("SELECT * FROM tag");

				while (rs.next()) {
					Tag tag = new Tag();
					tag.setId(rs.getInt(1));
					tag.setName(rs.getString(2));
					this.tags.add(tag);
				}
			}
			{
				// Documents & document tags
				rs = stmt.query("SELECT * FROM document");

				while (rs.next()) {
					Document doc = new ConcreteDocument();
					doc.setID(rs.getInt(1));
					Calendar cal = Calendar.getInstance();
					cal.setTimeInMillis(rs.getLong(2));
					doc.setCreation_date(cal);
					cal.setTimeInMillis(rs.getLong(3));
					doc.setLast_edit_date(cal);
					doc.setName(rs.getString(4));
					doc.setContents(rs.getString(5));
					{
						ResultSet rstags = stmt.query("SELECT * FROM document_tagged WHERE document_id="+doc.getID());
						while (rstags.next()) {
							for (Iterator iter = this.tags.iterator(); iter.hasNext();) {
								Tag tag = (Tag)iter.next();
								if (tag.getId() == rstags.getInt(2)) {
									doc.addTag(tag);
								}
							}
						}
						rstags = null;
					}
					System.out.println(doc.toString());
					this.documents.add(doc);
				}
			}
			rs = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Vector<Document> getAllDocuments() {
		return this.documents;
	}

	public Document getDocument(String name) {
		Document retdoc = null;
		for (Document doc : this.documents) {
			if (doc.getName().equals(name)) {
				System.out.println("returning " + doc.getName());
				retdoc = doc;
			}
		}

		return retdoc;
	}

	public void removeDocument(String name) {
		for (Document doc : this.documents) {
			if (doc.getName().equals(name)) {
				this.documents.remove(doc);
			}
		}
	}

	public void insertDocument(Document document) {
		this.documents.add(document);
	}

	public void updateDocument(Document document) {
		for (Document doc : this.documents) {
			if (doc.getName().equals(document.getName())) {
				int index = this.documents.indexOf(doc);
				this.documents.remove(index);
				this.documents.add(index, document);
			}
		}
	}
	
	public Document getDocumentById(int id) {
		Document resultDocument = new ConcreteDocument();
		for (Document doc : this.documents) {
			if (doc.getID() == id) {
				resultDocument = doc;
				}
		}
		return resultDocument;
	}

	public void commit() {
		// TODO
	}

}
